/*
 * CustomizerPanel.java
 *
 * Created on Nov 10, 2011, 10:27:50 PM
 */
package com.munian.ivy.module.ui.customizer;

import com.munian.ivy.module.exceptions.IvyException;
import com.munian.ivy.module.facade.IvyFacade;
import com.munian.ivy.module.options.IvyOptions;
import com.munian.ivy.module.preferences.EditablePreferences;
import com.munian.ivy.module.util.Utilities;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JCheckBox;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;
import org.openide.filesystems.FileChooserBuilder;
import org.openide.util.Exceptions;
import org.openide.util.NbBundle;

/**
 *
 */
public class CustomizerPanel extends javax.swing.JPanel {

    private EditablePreferences editablePreferences;
    private IvyOptions optionsLookup;
    private IvyFacade ivyFacade;
    private PanelActionListener actionListener = new PanelActionListener();
    private static final FileFilter PROPERTIS_FILE_FILTER = new FileNameExtensionFilter("properties", "properties");

    /**
     * Creates new form CustomizerPanel
     */
    public CustomizerPanel(EditablePreferences editablePreferences, IvyOptions optionsLookup, IvyFacade ivyFacade) {
        this.editablePreferences = editablePreferences;
        this.optionsLookup = optionsLookup;
        this.ivyFacade = ivyFacade;
        initComponents();
        loadPanel();
    }

    private void setOverridableOptionsState(boolean ivyEnabled, boolean useGlobal) {
        boolean enableChildComponents = !useGlobal;
        boolean enableCombo = useGlobal;
        if (!ivyEnabled) {
            enableChildComponents = false;
            enableCombo = false;
        }
        setChildComponentsState(overridableOptions1, enableChildComponents, retrieveSettingsTemplatesCb);
        retrieveSettingsTemplatesCb.setEnabled(enableCombo);
    }

    private void setupConfs(Collection<String> selectedConfs, boolean allSelected, boolean ivyEnabled) {
        if (ivyEnabled) {
            availableConfigurations.removeAll();
            try {
                String[] confs = ivyFacade.getConfs(ivyFileText.getText(), ivySettingsFileText.getText(), getPropertiesFilesAsList());
                for (String conf : confs) {
                    JCheckBox jcb = new JCheckBox(conf);
                    if (!allSelected && selectedConfs.contains(conf)) {
                        jcb.setSelected(true);
                    }
                    availableConfigurations.add(jcb, java.awt.BorderLayout.CENTER);
                }
            } catch (IvyException ex) {
                Exceptions.printStackTrace(ex);
            }
        }
    }

    private void setFilesTabState(boolean ivyEnabled, Object source) {
        setChildComponentsState(filesTabPanel, ivyEnabled, source);
        ivyPropertiesFileDeleteBtn.setEnabled(false);
        propertiesFileList.setEnabled(ivyEnabled);
    }

    private void setChildComponentsState(Container container, boolean enabled, Object source) {
        Component[] components = container.getComponents();
        for (Component component : components) {
            if (!component.equals(source)) {
                component.setEnabled(enabled);
            }
        }
    }

    private void setResolutionTabState(boolean ivyEnabled, Object source) {
        setChildComponentsState(resolutionSettingsTabPanel, ivyEnabled, source);
        setOverridableOptionsState(ivyEnabled, useRetrieveSettingsTemplateCbx.isSelected());
    }

    private void setConfigurationsTabState(boolean ivyEnabled, Object source) {
        setAvailableConfigrationTabState(ivyEnabled, allConfigurations.isSelected());
        setChildComponentsState(configurationsTabPanel, ivyEnabled, source);
    }

    private void setAvailableConfigrationTabState(boolean ivyEnabled, boolean allConfigurationsSelected) {
        boolean enableChildComponents = !allConfigurationsSelected;
        if (!ivyEnabled) {
            enableChildComponents = false;
        }
        setChildComponentsState(availableConfigurations, enableChildComponents, allConfigurations);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        enableIvyCbx = new javax.swing.JCheckBox();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        filesTabPanel = new javax.swing.JPanel();
        ivyFileLabel = new javax.swing.JLabel();
        ivyFileText = new javax.swing.JTextField();
        ivyFileBrowseBtn = new javax.swing.JButton();
        ivySettingsFileLabel = new javax.swing.JLabel();
        ivySettingsFileText = new javax.swing.JTextField();
        ivySettingsFileBrowseBtn = new javax.swing.JButton();
        ivyPropertiesFileLabel = new javax.swing.JLabel();
        ivyPropertiesFileAddBtn = new javax.swing.JButton();
        propertiesFileListScroll = new javax.swing.JScrollPane();
        propertiesFileList = new javax.swing.JList();
        ivyPropertiesFileDeleteBtn = new javax.swing.JButton();
        resolutionSettingsTabPanel = new javax.swing.JPanel();
        overridableOptions1 = new com.munian.ivy.module.ui.options.OverridableOptions();
        retrieveSettingsTemplatesCb = new javax.swing.JComboBox();
        useRetrieveSettingsTemplateCbx = new javax.swing.JCheckBox();
        autoResolveCheckBox = new javax.swing.JCheckBox();
        useCachePathCheckBox = new javax.swing.JCheckBox();
        configurationsTabPanel = new javax.swing.JPanel();
        allConfigurations = new javax.swing.JCheckBox();
        availableConfigurations = new javax.swing.JPanel();

        setLayout(new java.awt.BorderLayout());

        enableIvyCbx.setSelected(!editablePreferences.isIvyEnabled());
        enableIvyCbx.setText(org.openide.util.NbBundle.getMessage(CustomizerPanel.class, "CustomizerPanel.enableIvyCbx.text")); // NOI18N
        enableIvyCbx.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                enableIvyCbxItemStateChanged(evt);
            }
        });
        add(enableIvyCbx, java.awt.BorderLayout.PAGE_START);

        filesTabPanel.setLayout(new java.awt.GridBagLayout());

        ivyFileLabel.setText(org.openide.util.NbBundle.getMessage(CustomizerPanel.class, "CustomizerPanel.ivyFileLabel.text")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.weighty = 1.0;
        filesTabPanel.add(ivyFileLabel, gridBagConstraints);

        ivyFileText.setText(org.openide.util.NbBundle.getMessage(CustomizerPanel.class, "CustomizerPanel.ivyFileText.text")); // NOI18N
        ivyFileText.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                ivyFileTextFocusLost(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 10.0;
        gridBagConstraints.weighty = 1.0;
        filesTabPanel.add(ivyFileText, gridBagConstraints);

        ivyFileBrowseBtn.setText(org.openide.util.NbBundle.getMessage(CustomizerPanel.class, "CustomizerPanel.ivyFileBrowseBtn.text")); // NOI18N
        ivyFileBrowseBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ivyFileBrowseBtnActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.weighty = 1.0;
        filesTabPanel.add(ivyFileBrowseBtn, gridBagConstraints);

        ivySettingsFileLabel.setText(org.openide.util.NbBundle.getMessage(CustomizerPanel.class, "CustomizerPanel.ivySettingsFileLabel.text")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.weightx = 0.5;
        filesTabPanel.add(ivySettingsFileLabel, gridBagConstraints);

        ivySettingsFileText.setText(org.openide.util.NbBundle.getMessage(CustomizerPanel.class, "CustomizerPanel.ivySettingsFileText.text")); // NOI18N
        ivySettingsFileText.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                ivySettingsFileTextFocusLost(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 10.0;
        gridBagConstraints.weighty = 1.0;
        filesTabPanel.add(ivySettingsFileText, gridBagConstraints);

        ivySettingsFileBrowseBtn.setText(org.openide.util.NbBundle.getMessage(CustomizerPanel.class, "CustomizerPanel.ivySettingsFileBrowseBtn.text")); // NOI18N
        ivySettingsFileBrowseBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ivySettingsFileBrowseBtnActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.weighty = 1.0;
        filesTabPanel.add(ivySettingsFileBrowseBtn, gridBagConstraints);

        ivyPropertiesFileLabel.setText(org.openide.util.NbBundle.getMessage(CustomizerPanel.class, "CustomizerPanel.ivyPropertiesFileLabel.text")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.weighty = 1.0;
        filesTabPanel.add(ivyPropertiesFileLabel, gridBagConstraints);

        ivyPropertiesFileAddBtn.setText(org.openide.util.NbBundle.getMessage(CustomizerPanel.class, "CustomizerPanel.ivyPropertiesFileAddBtn.text")); // NOI18N
        ivyPropertiesFileAddBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ivyPropertiesFileAddBtnActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.weighty = 1.0;
        filesTabPanel.add(ivyPropertiesFileAddBtn, gridBagConstraints);

        propertiesFileListScroll.setOpaque(false);

        propertiesFileList.setModel(new javax.swing.DefaultListModel());
        propertiesFileList.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        propertiesFileList.setVisibleRowCount(5);
        propertiesFileList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                propertiesFileListValueChanged(evt);
            }
        });
        propertiesFileList.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                propertiesFileListFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                propertiesFileListFocusLost(evt);
            }
        });
        propertiesFileListScroll.setViewportView(propertiesFileList);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 10.0;
        gridBagConstraints.weighty = 1.0;
        filesTabPanel.add(propertiesFileListScroll, gridBagConstraints);

        ivyPropertiesFileDeleteBtn.setText(org.openide.util.NbBundle.getMessage(CustomizerPanel.class, "CustomizerPanel.ivyPropertiesFileDeleteBtn.text")); // NOI18N
        ivyPropertiesFileDeleteBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ivyPropertiesFileDeleteBtnActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.weighty = 1.0;
        filesTabPanel.add(ivyPropertiesFileDeleteBtn, gridBagConstraints);

        jTabbedPane1.addTab(org.openide.util.NbBundle.getMessage(CustomizerPanel.class, "CustomizerPanel.filesTabPanel.TabConstraints.tabTitle"), filesTabPanel); // NOI18N

        resolutionSettingsTabPanel.setLayout(new java.awt.GridBagLayout());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        resolutionSettingsTabPanel.add(overridableOptions1, gridBagConstraints);

        retrieveSettingsTemplatesCb.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 10.0;
        gridBagConstraints.weighty = 1.0;
        resolutionSettingsTabPanel.add(retrieveSettingsTemplatesCb, gridBagConstraints);

        useRetrieveSettingsTemplateCbx.setSelected(!editablePreferences.isUseGlobalRetrieveSettings());
        useRetrieveSettingsTemplateCbx.setText(org.openide.util.NbBundle.getMessage(CustomizerPanel.class, "CustomizerPanel.useRetrieveSettingsTemplateCbx.text")); // NOI18N
        useRetrieveSettingsTemplateCbx.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                useRetrieveSettingsTemplateCbxItemStateChanged(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        resolutionSettingsTabPanel.add(useRetrieveSettingsTemplateCbx, gridBagConstraints);

        autoResolveCheckBox.setSelected(!editablePreferences.isAutoResolve());
        autoResolveCheckBox.setText(org.openide.util.NbBundle.getMessage(CustomizerPanel.class, "CustomizerPanel.autoResolveCheckBox.text")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        resolutionSettingsTabPanel.add(autoResolveCheckBox, gridBagConstraints);

        useCachePathCheckBox.setText(org.openide.util.NbBundle.getMessage(CustomizerPanel.class, "CustomizerPanel.useCachePathCheckBox.text")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        resolutionSettingsTabPanel.add(useCachePathCheckBox, gridBagConstraints);

        jTabbedPane1.addTab(org.openide.util.NbBundle.getMessage(CustomizerPanel.class, "CustomizerPanel.resolutionSettingsTabPanel.TabConstraints.tabTitle"), resolutionSettingsTabPanel); // NOI18N

        configurationsTabPanel.setLayout(new java.awt.BorderLayout());

        allConfigurations.setSelected(!editablePreferences.isAutoResolve());
        allConfigurations.setText(org.openide.util.NbBundle.getMessage(CustomizerPanel.class, "CustomizerPanel.allConfigurations.text")); // NOI18N
        allConfigurations.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                allConfigurationsItemStateChanged(evt);
            }
        });
        configurationsTabPanel.add(allConfigurations, java.awt.BorderLayout.PAGE_START);
        configurationsTabPanel.add(availableConfigurations, java.awt.BorderLayout.CENTER);

        jTabbedPane1.addTab(org.openide.util.NbBundle.getMessage(CustomizerPanel.class, "CustomizerPanel.configurationsTabPanel.TabConstraints.tabTitle"), configurationsTabPanel); // NOI18N

        add(jTabbedPane1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void enableIvyCbxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_enableIvyCbxItemStateChanged
        boolean ivyEnabled = evt.getStateChange() == ItemEvent.SELECTED;
        setFilesTabState(ivyEnabled, evt.getSource());
        setResolutionTabState(ivyEnabled, evt.getSource());
        setChildComponentsState(this, ivyEnabled, evt.getSource());
        setConfigurationsTabState(ivyEnabled, evt.getSource());
    }//GEN-LAST:event_enableIvyCbxItemStateChanged

    private void useRetrieveSettingsTemplateCbxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_useRetrieveSettingsTemplateCbxItemStateChanged
        boolean useGlobal = evt.getStateChange() == ItemEvent.SELECTED;
        setOverridableOptionsState(enableIvyCbx.isSelected(), useGlobal);

    }//GEN-LAST:event_useRetrieveSettingsTemplateCbxItemStateChanged

    private void ivyPropertiesFileAddBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ivyPropertiesFileAddBtnActionPerformed
        FileChooserBuilder builder = new FileChooserBuilder(Utilities.FILE_CHOOSER_BUILDER_KEY);
        builder.setTitle(NbBundle.getMessage(CustomizerPanel.class, "CustomizerPanel.propertiesFileChooser.title"));
        builder.addFileFilter(PROPERTIS_FILE_FILTER);
        File ivyPropertiesFileChosen = builder.showOpenDialog();

        if (ivyPropertiesFileChosen != null) {
            try {
                DefaultListModel propertyFileListModel = (DefaultListModel) propertiesFileList.getModel();
                String filePath = ivyPropertiesFileChosen.getCanonicalPath();
                if (!propertyFileListModel.contains(filePath)) {
                    propertyFileListModel.addElement(filePath);
                }
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            }
        }
    }//GEN-LAST:event_ivyPropertiesFileAddBtnActionPerformed

    private void ivyPropertiesFileDeleteBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ivyPropertiesFileDeleteBtnActionPerformed
        int[] selectedItems = propertiesFileList.getSelectedIndices();
        DefaultListModel propertyFileListModel = (DefaultListModel) propertiesFileList.getModel();
        for (int i = 0; i < selectedItems.length; i++) {
            int j = selectedItems[i];
            propertyFileListModel.remove(j - i);

        }
        ivyPropertiesFileDeleteBtn.setEnabled(false);
    }//GEN-LAST:event_ivyPropertiesFileDeleteBtnActionPerformed

    private void ivyFileBrowseBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ivyFileBrowseBtnActionPerformed
        FileChooserBuilder builder = new FileChooserBuilder(Utilities.FILE_CHOOSER_BUILDER_KEY);
        builder.setTitle(NbBundle.getMessage(CustomizerPanel.class, "CustomizerPanel.ivyFileChooser.title"));
        File ivyFileChosen = builder.showOpenDialog();

        if (ivyFileChosen != null) {
            String filePath = ivyFileChosen.toURI().toString();
            if (isValidIvyFile(filePath)) {
                ivyFileText.setText(filePath);
                setupConfs(getAllSelectedConfs(), allConfigurations.isSelected(), true);
            }
        }
    }//GEN-LAST:event_ivyFileBrowseBtnActionPerformed

    private void ivySettingsFileBrowseBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ivySettingsFileBrowseBtnActionPerformed
        FileChooserBuilder builder = new FileChooserBuilder(Utilities.FILE_CHOOSER_BUILDER_KEY);
        builder.setTitle(NbBundle.getMessage(CustomizerPanel.class, "CustomizerPanel.ivySettingsFileChooser.title"));
        File ivyFileChosen = builder.showOpenDialog();

        if (ivyFileChosen != null) {
            String filePath = ivyFileChosen.toURI().toString();
            if (isValidIvySettingsFile(filePath)) {
                ivySettingsFileText.setText(filePath);
                setupConfs(getAllSelectedConfs(), allConfigurations.isSelected(), true);
            }
        }
    }//GEN-LAST:event_ivySettingsFileBrowseBtnActionPerformed

    private void propertiesFileListFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_propertiesFileListFocusLost
        if (!ivyPropertiesFileDeleteBtn.equals(evt.getOppositeComponent())) {
            ivyPropertiesFileDeleteBtn.setEnabled(false);
        }
    }//GEN-LAST:event_propertiesFileListFocusLost

    private void propertiesFileListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_propertiesFileListValueChanged
        ivyPropertiesFileDeleteBtn.setEnabled(true);
    }//GEN-LAST:event_propertiesFileListValueChanged

    private void propertiesFileListFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_propertiesFileListFocusGained
        if (propertiesFileList.getSelectedIndex() >= 0) {
            ivyPropertiesFileDeleteBtn.setEnabled(true);
        }
    }//GEN-LAST:event_propertiesFileListFocusGained

    private void ivySettingsFileTextFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_ivySettingsFileTextFocusLost
        if (!isValidIvySettingsFile(ivySettingsFileText.getText())) {
            ivySettingsFileText.requestFocusInWindow();
            ivySettingsFileText.setText("");
        }
    }//GEN-LAST:event_ivySettingsFileTextFocusLost

    private void ivyFileTextFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_ivyFileTextFocusLost
        if (!isValidIvyFile(ivyFileText.getText())) {
            ivyFileText.requestFocusInWindow();
            ivyFileText.setText("");
        }
    }//GEN-LAST:event_ivyFileTextFocusLost

    private void allConfigurationsItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_allConfigurationsItemStateChanged
        boolean allSelected = evt.getStateChange() == ItemEvent.SELECTED;
        setAvailableConfigrationTabState(enableIvyCbx.isSelected(), allSelected);
    }//GEN-LAST:event_allConfigurationsItemStateChanged

    private boolean isValidIvySettingsFile(String filePath) {
        try {
            ivyFacade.testIvySettings(filePath, getPropertiesFilesAsList());
        } catch (IvyException ex) {
            NotifyDescriptor message = new NotifyDescriptor.Message(NbBundle.getMessage(CustomizerPanel.class, "CustomizerPanel.ivySettingsFile.Error") + ex.getMessage(), NotifyDescriptor.ERROR_MESSAGE);
            DialogDisplayer.getDefault().notify(message);
            return false;
        }
        return true;
    }

    private boolean isValidIvyFile(String filePath) {
        try {
            ivyFacade.testIvyFile(filePath, ivySettingsFileText.getText(), getPropertiesFilesAsList());
        } catch (IvyException ex) {
            NotifyDescriptor message = new NotifyDescriptor.Message(NbBundle.getMessage(CustomizerPanel.class, "CustomizerPanel.ivyFile.Error") + ex.getMessage(), NotifyDescriptor.ERROR_MESSAGE);
            DialogDisplayer.getDefault().notify(message);
            return false;
        }
        return true;
    }

    private List<String> getPropertiesFilesAsList() {
        List<String> propertyFiles = new ArrayList<String>();
        for (int i = 0; i < propertiesFileList.getModel().getSize(); i++) {
            propertyFiles.add((String) propertiesFileList.getModel().getElementAt(i));
        }
        return propertyFiles;
    }

    private void loadPanel() {
        autoResolveCheckBox.setSelected(editablePreferences.isAutoResolve());
        useRetrieveSettingsTemplateCbx.setSelected(editablePreferences.isUseGlobalRetrieveSettings());

        ivyFileText.setText(editablePreferences.getIvyFileString());
        ivySettingsFileText.setText(editablePreferences.getIvySettingsString());
        DefaultListModel propertyFileListModel = (DefaultListModel) propertiesFileList.getModel();
        for (String propertyFile : editablePreferences.getIvyPropertiesFilesString()) {
            propertyFileListModel.addElement(propertyFile);
        }
        retrieveSettingsTemplatesCb.setModel(new javax.swing.DefaultComboBoxModel(optionsLookup.getRetrieveSettingsTemplateNames().toArray()));
        overridableOptions1.loadPanel(editablePreferences.getProjectRetrieveSettings());
        useCachePathCheckBox.setSelected(editablePreferences.isUseCachePath());
        
        setupConfs(editablePreferences.getSelectedConfs(), editablePreferences.isAllConfsSelected(), editablePreferences.isIvyEnabled());
        allConfigurations.setSelected(editablePreferences.isAllConfsSelected());
        enableIvyCbx.setSelected(editablePreferences.isIvyEnabled());
    }

    private Collection<String> getAllSelectedConfs() {
        Collection<String> selectedConfs = new ArrayList<String>();
        Component[] components = availableConfigurations.getComponents();
        for (Component component : components) {
            if (component instanceof JCheckBox) {
                JCheckBox cbx = (JCheckBox) component;
                if (cbx.isSelected()) {
                    selectedConfs.add(cbx.getText());
                }
            }
        }
        return selectedConfs;
    }

    /**
     * @return the actionListener
     */
    public PanelActionListener getActionListener() {
        return actionListener;
    }

    public class PanelActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            editablePreferences.setIvyEnabled(enableIvyCbx.isSelected());
            editablePreferences.setAutoResolve(autoResolveCheckBox.isSelected());
            editablePreferences.setIvyFile(ivyFileText.getText());
            editablePreferences.setUseGlobalRetrieveSettings(useRetrieveSettingsTemplateCbx.isSelected());
            editablePreferences.setGlobalRetrieveSettingsName((String) retrieveSettingsTemplatesCb.getSelectedItem());
            editablePreferences.setIvySettingsFile(ivySettingsFileText.getText());
            editablePreferences.setUseCachePath(useCachePathCheckBox.isSelected());
            editablePreferences.setAllConfsSelected(allConfigurations.isSelected());

            editablePreferences.getSelectedConfs().clear();
            editablePreferences.getSelectedConfs().addAll(getAllSelectedConfs());

            overridableOptions1.readPanel(editablePreferences.getProjectRetrieveSettings());

            editablePreferences.clearPropertiesFiles();
            for (int i = 0; i < propertiesFileList.getModel().getSize(); i++) {
                editablePreferences.addPropertyFile((String) propertiesFileList.getModel().getElementAt(i));
            }

            editablePreferences.saveProjectPreferences();
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox allConfigurations;
    private javax.swing.JCheckBox autoResolveCheckBox;
    private javax.swing.JPanel availableConfigurations;
    private javax.swing.JPanel configurationsTabPanel;
    private javax.swing.JCheckBox enableIvyCbx;
    private javax.swing.JPanel filesTabPanel;
    private javax.swing.JButton ivyFileBrowseBtn;
    private javax.swing.JLabel ivyFileLabel;
    private javax.swing.JTextField ivyFileText;
    private javax.swing.JButton ivyPropertiesFileAddBtn;
    private javax.swing.JButton ivyPropertiesFileDeleteBtn;
    private javax.swing.JLabel ivyPropertiesFileLabel;
    private javax.swing.JButton ivySettingsFileBrowseBtn;
    private javax.swing.JLabel ivySettingsFileLabel;
    private javax.swing.JTextField ivySettingsFileText;
    private javax.swing.JTabbedPane jTabbedPane1;
    private com.munian.ivy.module.ui.options.OverridableOptions overridableOptions1;
    private javax.swing.JList propertiesFileList;
    private javax.swing.JScrollPane propertiesFileListScroll;
    private javax.swing.JPanel resolutionSettingsTabPanel;
    private javax.swing.JComboBox retrieveSettingsTemplatesCb;
    private javax.swing.JCheckBox useCachePathCheckBox;
    private javax.swing.JCheckBox useRetrieveSettingsTemplateCbx;
    // End of variables declaration//GEN-END:variables
}
